package com.jotta.client;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jotta.client.communicate.Configuration;
import com.jotta.client.dataaccess.DataAccess;
import com.jotta.client.setting.ModeOfApplication;
import com.jotta.client.setting.SettingManager;
import com.jotta.client.utility.Utility;

public class Setting extends PreferenceActivity {
	public SeekBar slider;
	private String tmpText = "";
	private long max_storage_size, min_storage_size;
	private TextView currentText;
	private Dialog dialog;
	Preference settingCachePref;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		
		Configuration.prepareConfiguration(this);
		
		initSettingInfo();

		handleStorageClickListener();
	}

	private void initSettingInfo() {
		final SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());

		CheckBoxPreference autoLoginPref = (CheckBoxPreference) findPreference("preferences_autologin");
		autoLoginPref.setChecked(SettingManager.getInstance().getAutoLogin());

		autoLoginPref
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						// TODO Auto-generated method stub
						SettingManager
								.getInstance()
								.setAutoLogin(
										prefs.getBoolean(
												"preferences_autologin", false));
						return false;
					}
				});

		Preference usernamePref = (Preference) findPreference("preferences_username");
		usernamePref.setSummary(SettingManager.getInstance().getUsername());

		Preference languagePref = (Preference) findPreference("preferences_language");
		languagePref.setSummary(SettingManager.getInstance().getLanguage());

		Preference versionPref = (Preference) findPreference("preferences_version");
		versionPref.setSummary(SettingManager.getInstance().getVersion(
				getBaseContext(), Preference.class));

		Preference freespacePref = (Preference) findPreference("preferences_free_space_on_jotta");
		tmpText = Utility.convertToStringRepresentation(SettingManager
				.getInstance().getServerFreeSpace());
		freespacePref.setSummary(tmpText);

		Preference usagePref = (Preference) findPreference("preferences_used_space_on_jotta");
		tmpText = Utility.convertToStringRepresentation(SettingManager
				.getInstance().getServerUsage());
		usagePref.setSummary(tmpText);

		Preference localCachePref = (Preference) findPreference("preferences_used_local_file_cache");
		tmpText = Utility.convertToStringRepresentation(SettingManager
				.getInstance().getLocalUsage());
		localCachePref.setSummary(tmpText);

		settingCachePref = (Preference) findPreference("preferences_max_space_on_device");
		tmpText = Utility.convertToStringRepresentation(SettingManager
				.getInstance().getLocalStorageCapacity());
		settingCachePref.setSummary(tmpText);

	}

	private void handleStorageClickListener() {
		Preference maxSpacePref = (Preference) findPreference("preferences_max_space_on_device");
		maxSpacePref
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference pref) {
						if (Configuration.getModeOfApplication() == ModeOfApplication.BROWSE) {
							String message = getResources().getString(
									R.string.browse_mode_message);
							Utility.getAlertDialog(Setting.this,
                                    getResources(), message).show();
							return true;
						} else if (!DataAccess.getInstance()
								.checkSDCardIsMounted()) {
							String message = getResources().getString(
									R.string.browse_mode_message);
							Utility.getAlertDialog(Setting.this,
                                    getResources(), message).show();
							Configuration
									.setModeOfApplication(ModeOfApplication.BROWSE);
							return true;
						} else {
							dialog = new Dialog(Setting.this);
							dialog.setContentView(R.layout.seekbar);
							dialog.setTitle("Storage");

							currentText = (TextView) dialog
									.findViewById(R.id.storage_current);
							long setting_storage_size = SettingManager
									.getInstance().getLocalStorageCapacity();
							tmpText = Utility
									.convertToStringRepresentation(setting_storage_size);
							currentText.setText(tmpText);

							TextView maxText = (TextView) dialog
									.findViewById(R.id.storage_max);
							max_storage_size = SettingManager.getInstance()
									.getMaxSizeOfStorage();
							tmpText = Utility
									.convertToStringRepresentation(max_storage_size);
							maxText.setText(tmpText);

							TextView minText = (TextView) dialog
									.findViewById(R.id.storage_current);
							min_storage_size = SettingManager.getInstance()
									.getLocalUsage();
							tmpText = Utility
									.convertToStringRepresentation(min_storage_size);
							minText.setText(tmpText);

							long progress = ((setting_storage_size - min_storage_size) * 100)
									/ (max_storage_size - min_storage_size);
							slider = (SeekBar) dialog.findViewById(R.id.slider);
							slider.setProgress((int) progress);
							slider.setOnSeekBarChangeListener(seekBarChangeListener);

							Button btnDone = (Button) dialog
									.findViewById(R.id.Done);
							btnDone.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {

									long newSettingStorage = min_storage_size
											+ (slider.getProgress() * (max_storage_size - min_storage_size))
											/ 100;
									SettingManager.getInstance()
											.setLocalStorageCapacity(
													newSettingStorage);
									tmpText = Utility
											.convertToStringRepresentation(newSettingStorage);
									settingCachePref.setSummary(tmpText);
									dialog.dismiss();

								}
							});

							Button btnCancel = (Button) dialog
									.findViewById(R.id.Cancel);
							btnCancel.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});

							dialog.show();
							return true;
						}
					}
				});
	}

	private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			currentText
					.setText(Utility
							.convertToStringRepresentation(min_storage_size
									+ (progress * (max_storage_size - min_storage_size))
									/ 100));
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			;
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			;

		}
	};

}
